package com.luck.easyfoodbatch.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.GoodDetail;
import com.luck.easyfoodbatch.preview.ImageInfo;
import com.luck.easyfoodbatch.preview.ImagePreviewActivity;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyfoodbatch.verticalslide.CustListView;
import com.zzhoujay.richtext.RichText;
import com.zzhoujay.richtext.callback.OnImageClickListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.fragment
 * email：893855882@qq.com
 * data：16/9/30
 */
public class ProductDetailsFragment extends BaseFragment {
    @BindView(R.id.listview)
    CustListView listview;
    @BindView(R.id.iv_nodata)
    ImageView iv_nodata;
    private View rootView;

    private String gid = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fc_product, container, false);
            ButterKnife.bind(this, rootView);
            initView();
        }

        return rootView;
    }

    private void initView() {
        gid = getActivity().getIntent().getStringExtra("gid");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 接口请求返回数据
     */
    public void initTextData(GoodDetail.DataBean data) {
        if (data != null) {
            String content = data.getContent();
            if (!Tools.isNull(content)) {
                if (iv_nodata != null)
                    iv_nodata.setVisibility(View.INVISIBLE);
                try {
                    if (listview != null)
                        listview.setAdapter(new ProductAdapter(content));
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                if (iv_nodata != null)
                    iv_nodata.setVisibility(View.VISIBLE);
            }
        } else {
            if (iv_nodata != null)
                iv_nodata.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 商品详情
     */
    private class ProductAdapter extends BaseAdapter {
        private String content;

        public ProductAdapter(String content) {
            super();
            this.content = content;
        }

        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(mContext).inflate(R.layout.item_product_list, null);
                holder.text = (TextView) view.findViewById(R.id.text_item);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            RichText.from(content).imageClick(new OnImageClickListener() {
                @Override
                public void imageClicked(List<String> imageUrls, int position, View widget) {
                }
            }).into(holder.text);
            return view;
        }

        class Holder {
            TextView text;
        }
    }
}
